package com.wxy.care.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wxy.care.entity.Falldown;
import com.wxy.care.mapper.FalldownMapper;
import com.wxy.care.service.FalldownService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class FalldownServiceImpl extends ServiceImpl<FalldownMapper,Falldown> implements FalldownService {
    @Override
    public boolean fallDownSet(String houseName, String slaveId, Boolean flag) {
        Falldown fd = new Falldown();
        fd.setDate(LocalDateTime.now());
        fd.setHouseName(houseName);
        fd.setSlaveId(slaveId);
        fd.setFlag(flag);
        return this.save(fd);
    }

    @Override
    public Boolean isFalldown(String houseName) {
        Falldown falldown = null;
        try {
            falldown = this.getOne(Wrappers.<Falldown>lambdaQuery()
                    .eq(Falldown::getHouseName, houseName)
                    .orderByDesc(Falldown::getDate)
                    .last("LIMIT 1"));
        } catch (Exception e) {
            return false;
        }
        if(falldown == null)
            return false;
        return falldown.getFlag();
    }
}
